/*
实名身份验证功能，该功能测试通过但并未正式的使用
*/
var confige = require("confige");

cc.Class({
    extends: cc.Component,

    properties: {
        isInit:false
    },

    onLoad: function () {

    },

    onInit:function(){
        this.trueColor = new cc.Color(0, 0, 0);
        this.falseColor = new cc.Color(255, 0, 0);
        this.checkImgList = [];
        this.checkStatusList = [];
        this.checkEditList = [];
        this.editLayer = this.node.getChildByName("editLayer");
        this.phoneEdit = this.editLayer.getChildByName("node0").getChildByName("editBox").getComponent("cc.EditBox");
        this.checkCodeEdit = this.editLayer.getChildByName("node1").getChildByName("editBox").getComponent("cc.EditBox");
        this.idEdit = this.editLayer.getChildByName("node2").getChildByName("editBox").getComponent("cc.EditBox");
        this.nameEdit = this.editLayer.getChildByName("node3").getChildByName("editBox").getComponent("cc.EditBox");
        for(var i=0;i<4;i++){
            this.checkImgList[i] = this.editLayer.getChildByName("node"+i).getChildByName("checkImg");
            this.checkStatusList[i] = false;
        }
        this.checkEditList = [this.phoneEdit,this.checkCodeEdit,this.idEdit,this.nameEdit];

        this.btnGetCode = this.editLayer.getChildByName("node0").getChildByName("btnGetCode").getComponent("cc.Button");
        this.btnCheck = this.editLayer.getChildByName("btnCheck").getComponent("cc.Button");
        this.btnCheck.interactable = false;
        this.btnGetCode.interactable = false;

        this.curLabel = this.node.getChildByName("curLabel").getComponent("cc.Label");
        this.curPhone = "";
        this.curID = "";
        this.curName = "";

        if(confige.userInfo.identityInfo && confige.userInfo.identityInfo.cardNo != 0)
        {
            this.curPhone = confige.userInfo.identityInfo.phone;
            this.curID = confige.userInfo.identityInfo.cardNo;
            this.curName = confige.userInfo.identityInfo.realName;
            this.showInfo();
        }

        this.isInit = true;
    },

    btnGetCodeClick:function(){
        var self = this;
        var phone = parseInt(this.phoneEdit.string);
        pomelo.request("connector.identity.sendCaptcha", {"phone" : phone}, function(data) {
            console.log("flag is : ")
            console.log(data);
            self.curPhone = phone;
        });
    },

    btnCheckClick:function(){
        var self = this;
        var code = parseInt(this.checkCodeEdit.string);
        var cardNo = this.idEdit.string;
        var realName = this.nameEdit.string;
        pomelo.request("connector.identity.bindPhone", {"code" : code,"cardNo" : cardNo,"realName" : realName}, function(data) {
            console.log("flag is : ")
            console.log(data);
            if(data.flag.error_code == 0)
            {
                self.curID = cardNo;
                self.curName = realName;
                self.showInfo();
            }
        });
    },

    checkEditBox:function(event, customEventData){
        var index = parseInt(customEventData);
        var curStr = "";
        if(index == 0)
        {
            curStr = this.checkEditList[index].string;
            if(curStr.length == 11 && curStr[0] == "1"){
                this.checkTrue(index);
                this.btnGetCode.interactable = true;
            }else{
                this.checkFalse(index);
                this.btnGetCode.interactable = false;
            }
        }
        if(index == 1)
        {
            curStr = this.checkEditList[index].string;
            if(curStr.length == 4)
                this.checkTrue(index);
            else
                this.checkFalse(index);
        }
        if(index == 2)
        {
            curStr = this.checkEditList[index].string;
            if(this.checkID(curStr))
                this.checkTrue(index);
            else
                this.checkFalse(index);
        }
        if(index == 3)
        {
            curStr = this.checkEditList[index].string;
            if(this.checkName(curStr))
                this.checkTrue(index);
            else
                this.checkFalse(index);
        }

        if(this.checkStatusList[0] && this.checkStatusList[1] && this.checkStatusList[2] && this.checkStatusList[3])
            this.btnCheck.interactable = true;
        else
            this.btnCheck.interactable = false;
    },

    checkTrue:function(index){
        console.log("通过验证===="+index);
        this.checkImgList[index].active = true;
        this.checkStatusList[index] = true;
        this.checkEditList[index].fontColor = this.trueColor;
    },
    checkFalse:function(index){
        console.log("没通过验证====="+index);
        this.checkImgList[index].active = false;
        this.checkStatusList[index] = false;
        this.checkEditList[index].fontColor = this.falseColor;
    },

    showLayer:function(){
        if(this.isInit == false)
            this.onInit();
        this.node.active = true;
    },

    hideLayer:function(){
        this.node.active = false;
    },

    showInfo:function(){
        this.editLayer.active = false;
        this.curLabel.string = "姓名:\n"+this.curName+"\n"+"身份证号:"+this.curID+"\n"+"手机号:\n"+this.curPhone;
    },

    checkName:function(str){
        console.log("checkName===="+str);
        var regName = /^[\u4E00-\u9FA5]{2,4}$/;
        if(!regName.test(str)){
            console.log('真实姓名填写有误');
            return false;
        }else{
            return true;
        }
    },
    checkID:function(str){
        console.log("checkID===="+str);
        var regIdNo = /^\d{17}([0-9]|X)$/;
        if(!regIdNo.test(str)){
            console.log('身份证号填写有误');
            return false;
        }else{
            return true;
        }
    },
});
